//
//  TSLoginViewController.m
//  chart
//
//  Created by xu on 15/7/21.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSLoginViewController.h"
#import "TSRegisterViewController.h"
#import "TSHomeViewController.h"

#define MARGIN_TOP_LOGIN      24
#define MARGIN_LEFT_LOGIN     24
#define FLD_HEIGHT      41
#define BTN_HEIGHT      56

#define TXT_COLOR       RGB(168, 168, 168)
#define BTN_COLOR       RGB(107, 186, 81)

@interface TSLoginViewController ()<UITextFieldDelegate>{
    UITextField             *phoneNumField;
    UITextField             *passwordField;
    UIButton                *loginBtn;
    UIButton                *registerBtn;
}

@end

@implementation TSLoginViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = @"欢迎登录";
    
    self.view.backgroundColor = RGB(239, 240, 244);
    
    [self leftBarButtonWithTitle:nil barNorImage:nil barHltImage:nil action:nil];
    
    /* 加入所有控件 */
    [self addUIComponent];
}

- (void)addUIComponent{
    
    /* 加入手机号和密码输入框 */
    [self addPhoneAndPwdView];
    
    /* 加入登录按钮 */
    [self addLoginBtn];
    
    /* 加入注册按钮 */
    [self addRegisterBtn];
    
    /* 加入收键盘手势 */
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleField:)];
    [self.view addGestureRecognizer:tap];
}

- (void)handleField:(UIGestureRecognizer*)gesture{
    
    for (UIView * view in self.view.subviews) {
        if ([view isFirstResponder]) {
            [view resignFirstResponder];
        }
    }
}

/* 加入手机号和密码输入框 */
- (void)addPhoneAndPwdView
{
    phoneNumField = [[UITextField alloc] init];
    phoneNumField.placeholder = @"请输入用户名";
    phoneNumField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setUpFieldProperties:phoneNumField];
    phoneNumField.keyboardType = UIKeyboardTypeASCIICapable;
    if ([USERDEFAULTS objectForKey:TSUserDefaultsKey_UserName]) {
        [phoneNumField setText:[USERDEFAULTS objectForKey:TSUserDefaultsKey_UserName]];
    }
    [self.view addSubview:phoneNumField];
    
    passwordField = [[UITextField alloc] init];
    passwordField.placeholder = @"请输入密码";
    passwordField.secureTextEntry  = YES;
    passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self setUpFieldProperties:passwordField];
    passwordField.keyboardType = UIKeyboardTypeASCIICapable;
    if ([USERDEFAULTS objectForKey:TSUserDefaultsKey_Password]) {
        [passwordField setText:[USERDEFAULTS objectForKey:TSUserDefaultsKey_Password]];
    }
    [self.view addSubview:passwordField];
}

/* 加入登录按钮 */
- (void)addLoginBtn
{
    loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.titleLabel.font = FONT(12);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor grayColor]];
    [loginBtn.layer setCornerRadius:5.f];
    [loginBtn addTarget:self action:@selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}

/* 加入注册按钮 */
- (void)addRegisterBtn
{
    registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.titleLabel.font = FONT(12);
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:[UIColor grayColor]];
    [registerBtn.layer setCornerRadius:5.f];
    [registerBtn addTarget:self action:@selector(registerUser:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
}

/* 右上角按钮点击注册 */
- (void)registerUser:(UIButton *)button
{
    TSRegisterViewController *register_vc = [[TSRegisterViewController alloc] init];
    [self.navigationController pushViewController:register_vc animated:YES];
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    phoneNumField.frame = CGRectMake(MARGIN_LEFT_LOGIN,
                                     MARGIN_TOP_LOGIN,
                                     CGRectGetWidth(self.view.frame)-2*MARGIN_LEFT_LOGIN,
                                     FLD_HEIGHT);
    passwordField.frame = CGRectMake(MARGIN_LEFT_LOGIN,
                                     MARGIN_TOP_LOGIN*2+FLD_HEIGHT,
                                     CGRectGetWidth(phoneNumField.frame),
                                     FLD_HEIGHT);
    loginBtn.frame = CGRectMake(MARGIN_LEFT_LOGIN, (MARGIN_TOP_LOGIN+FLD_HEIGHT)*2+MARGIN_TOP_LOGIN, CGRectGetWidth(passwordField.frame), FLD_HEIGHT);
    
    registerBtn.frame = CGRectMake(MARGIN_LEFT_LOGIN, CGRectGetMaxY(loginBtn.frame)+MARGIN_TOP_LOGIN, CGRectGetWidth(passwordField.frame), FLD_HEIGHT);
}

/* login click */
- (void)loginButtonClicked:(UIButton*)sender{
    WS(weakSelf);
    [[TSUserModel sharedInstance] loginWithUser:phoneNumField.text andPassword:passwordField.text completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
       
        if (isSucceeded) {
            TSHomeViewController *home_vc = [[TSHomeViewController alloc] init];
            [weakSelf.navigationController pushViewController:home_vc animated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_HasLogined object:nil];
            
            
        }
    }];
}

/* 设置textField属性 */
- (void)setUpFieldProperties:(UITextField *)field{
    field.backgroundColor =[UIColor whiteColor];
    field.tintColor = [UIColor grayColor];
    field.font = FONT(12);
    field.layer.cornerRadius = 5.f;
    field.layer.borderColor = [UIColor grayColor].CGColor;
    field.layer.borderWidth = .5f;
    field.delegate = self;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 5)];
    field.leftView = view;
    field.leftViewMode = UITextFieldViewModeAlways;
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

@end
